package com.example.testservice;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	Button start;
	Button stop;
	Button bind;
	Button unbind;

	MyService.MyBinder binder;

	private final ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName arg0, IBinder service) {
			Log.i("gjf", "ServiceConnection++++++++++onServiceConnected");
			binder = (MyService.MyBinder) service;
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			Log.i("gjf", "ServiceConnection++++++++++onServiceDisconnected");
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		start = (Button) findViewById(R.id.service_start);
		stop = (Button) findViewById(R.id.service_stop);
		bind = (Button) findViewById(R.id.service_bind);
		unbind = (Button) findViewById(R.id.service_unbind);

		final Intent intent1 = new Intent(getApplicationContext(),
				MyService.class);
		final Intent intent2 = new Intent(getApplicationContext(),
				MyIntentService.class);

		start.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Log.i("gjf", "startService++++++++++");
				startService(intent2);
			}
		});

		stop.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Log.i("gjf", "stopService++++++++++");
				stopService(intent2);
			}
		});

		bind.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Log.i("gjf", "bindService++++++++++");
				bindService(intent1, conn, BIND_AUTO_CREATE);
			}
		});

		unbind.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Log.i("gjf", "unbindService++++++++++");
				unbindService(conn);
			}
		});
	}
}
